package com.cl.park.action;

import java.util.List;
import java.util.Map;

import lombok.Data;
import lombok.EqualsAndHashCode;

import com.cl.park.model.Admin;
import com.cl.park.model.AjaxData;
import com.cl.park.model.AjaxResponse;
import com.cl.park.model.Car;
import com.cl.park.service.AdminService;
import com.cl.park.service.VpService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@Data
@EqualsAndHashCode(callSuper=false)
public class AdminAction extends ActionSupport{
	private static final long serialVersionUID = 1L;
	public AdminService adminService;
	public Admin admin;
	public String msg;
	public List<Object> list;
	public Integer id;
	public AjaxData<Object> data;
	
	public AjaxResponse result;
	
	private Car car;
	private VpService vp;
	
	public String index(){
		return isOnline()?SUCCESS:LOGIN;
	}
	
	//判断SESSION是否存在
	public boolean isOnline(){
		Map<String,Object> session = ActionContext.getContext().getSession();
		if(session.get("admin")==null){
			return false;
		}
		return true;
	}
	
	//用户登录
	public String AdminLogin(){
		if(admin.getUserName().equals("") ||admin.getPassWord().equals("")){
			return "none";
		}
		Admin now = this.adminService.adminLogin(admin.getUserName(), admin.getPassWord());
		
		
		if(now == null){
			return "none";
		}
		ActionContext.getContext().getSession().put("admin", now);
		if(now.getType() == 1){
			return "index";
		}
		if(now.getType() == 2){
			return "vp";
		}
		return SUCCESS;
	}
	
	//添加操作员
	public String addVP(){
		
		if(!isOnline()){
			return "login";
		}
		if(admin.getUserName().equals("") || admin.getPassWord().equals("")){
			return "none";
		}
		this.setMsg(this.adminService.addVP(admin));
		return "vp";
	}
	
	//跳转到修改操作员信息页面
	public String toUpdateAdmin(){
		if(!isOnline()){
			return "login";
		}
		this.setAdmin(this.adminService.getAdmin(admin.getId()));
		return SUCCESS;
	}
	
	//更新操作员信息
	public String updateAdmin(){
		if(!isOnline()){
			return "login";
		}
		this.adminService.updateAdmin(admin);
		return SUCCESS;
	}
	
	public String logout(){
		ActionContext.getContext().getSession().remove("admin");
		return SUCCESS;
	}
	
	//操作员列表
	public String list(){
		if(!isOnline()){
			return "login";
		}
		this.setList(this.adminService.adminList());
		return "list";
	}
	
	public String getOperatorList(){
		if(!isOnline()){
			return "login";
		}
		data = new AjaxData<Object>();
		data.setData(adminService.adminList());
		return SUCCESS;
	}
	
	public String edit(){
		if(null != id){
			admin = adminService.getAdmin(id);
		}else{
			admin = null;
		}
		id = null;
		return SUCCESS;
	}
	
	public String save(){
		if(null != admin && null != admin.getId()){
			adminService.updateAdmin(admin);
			result = new AjaxResponse(true, "修改成功");
		}else{
			adminService.addVP(admin);
			result = new AjaxResponse(true, "添加成功");
		}
		return SUCCESS;
	}
	
	
	/**
	 * 查看日志
	 * @return
	 */
	public String listLog(){
		return SUCCESS;
	}
	
	public String getLogList(){
		if(null == car){
			data = new AjaxData<Object>(0, 0, this.vp.lookLog(null));
		}else{
			data = new AjaxData<Object>(0, 0, this.vp.lookLog(car.getCarNo()));
		}
		
		return SUCCESS;
	}
	
}
